import { Point, Shape } from "./types";
import GameConfig from "./GameConfig";
import { SquareGroup } from "./SquareGroup";

/**
 * 该类提供一系列的函数，根据游戏规则判断各种情况
 */
export class TerisRule {
  /**
   * 判断方块能否移动到某个为止
   * @param shape
   * @returns
   */
  static canIMove(shape: Shape, targetPoint: Point): boolean {
    // 假设中心点已经移动到了目标位置，算出每个小方块的坐标
    const targetSquarePoints: Point[] = shape.map((v) => {
      return {
        x: v.x + targetPoint.x,
        y: v.y + targetPoint.y,
      };
    });
    // 边界判断
    const result = targetSquarePoints.some((v) => {
      // 是否超出边界
      return (
        v.x < 0 ||
        v.x > GameConfig.pannelSize.width - 1 ||
        v.y < 0 ||
        v.y > GameConfig.pannelSize.height - 1
      );
    });

    if (result) {
      return false;
    }
    return true;
  }

  /**
   * 判断方块能否旋转
   * @param shape
   * @returns
   */
  static rotate(teris: SquareGroup): boolean {
    const newShape = teris.afterRotateShape() //得到旋转之后新的形状
    if(this.canIMove(newShape, teris.centerPoint)){
      teris.rotate()
      return true
    }else{
      return false
    }
  }
}
